package com.biu.billiards.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.biu.billiards.config.BaseErrorEnum;
import com.biu.billiards.config.BaseException;
import com.biu.billiards.mapper.DyMapper;
import com.biu.billiards.model.bo.DyBO;
import com.biu.billiards.model.entity.*;
import com.biu.billiards.model.vo.dy.DyGoodsVo;
import com.biu.billiards.model.vo.dyVo.AddDyVo;
import com.biu.billiards.model.vo.dyVo.GetDyVo;
import com.biu.billiards.model.vo.dyVo.UpdateDyVO;
import com.biu.billiards.model.vo.mqtt.OpenBilliardsVo;
import com.biu.billiards.model.vo.shop.orderAdmin.AddOrderAdminVo;
import com.biu.billiards.service.*;
import com.biu.billiards.utils.DyUtils;
import com.biu.billiards.utils.MtUtils;
import com.biu.billiards.utils.Tool;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author xue
 * @since 2023-09-25
 */
@Service
public class DyServiceImpl  extends ServiceImpl<DyMapper, com.biu.billiards.model.entity.Dy> implements IDyService  {

    @Resource
    private IBilliardsService billiardsService;

    @Resource
    private IOrderService orderService;
    @Resource
    private IGoodsService goodsService;

    @Resource
    private IShopService shopService;

    @Resource
    private IUserService userService;


    @Resource
    private DyMapper dyMapper;


    @Resource
    private IHardwareService hardwareService;

    @Resource
    private IMtService mtService;


    @Resource
    private IDyService dyService;



    @Override
    public Boolean dyValid(DyGoodsVo dyGoodsVo) {



        if (billiardsService.getBilliardsOpenStatus(dyGoodsVo.getBilliardsId())){
            throw new BaseException(BaseErrorEnum.OPEN_ERROR);
        }

        Dy dy = dyService.selectDy(dyGoodsVo.getShopId());

        if (dy==null){
            return false;
        }
        System.out.println("dy42342352352352352352");
        System.out.println(dy);



        OpenBilliardsVo openBilliardsVo =  DyUtils.dyValid(dyGoodsVo,dy);


        if (openBilliardsVo!=null){

            System.out.println("验证了");

            AddOrderAdminVo addOrderVo = new AddOrderAdminVo();


            addOrderVo.setPhone(dyGoodsVo.getPhone());

            addOrderVo.setOrderId(Tool.getOrderNum());
            addOrderVo.setPlayWay("抖音核销");
            addOrderVo.setCreateTime(LocalDateTime.now());
            addOrderVo.setGoodsId(openBilliardsVo.getGoodsId());

            addOrderVo.setOrderStatus(1);
            addOrderVo.setUserId(0);

            addOrderVo.setBilliardsNumber(openBilliardsVo.getBilliardsNumber());
            addOrderVo.setDeviceNumber(openBilliardsVo.getDeviceNumber());
            addOrderVo.setBilliardsId(openBilliardsVo.getBilliardsId());


            addOrderVo.setShopId(dyGoodsVo.getShopId());
            addOrderVo.setUserId(dyGoodsVo.getUserId());




            Goods goods = goodsService.selectGoods(openBilliardsVo.getGoodsId());


            if (goods!=null){

                //hardware_id
               // Hardware hardware = hardwareService.getSubHardware(dyGoodsVo.getShopId());
                //hardware_id


                addOrderVo.setGoodsName(goods.getGoodsName());
                addOrderVo.setActualPrice( goods.getGoodsPrice());
                addOrderVo.setBilliardsHour(goods.getPlayHour());

                addOrderVo.setStartTime(LocalDateTime.now());
                addOrderVo.setEndTime(LocalDateTime.now().plusHours(goods.getPlayHour()));
                openBilliardsVo.setHour(goods.getPlayHour());

               // dyGoodsVo.getHardwareId()
                openBilliardsVo.setHardwareId(dyGoodsVo.getHardwareId());


                openBilliardsVo.setShopId(dyGoodsVo.getShopId());
               // openBilliardsVo.setHardwareId();

              //  hardwareService.getSubHardware(goods.getShopId());


                //抖音核销 1 年费 2 分成
                Shop shop = shopService.getShop(dyGoodsVo.getShopId());


                if (shop!=null){
                    addOrderVo.setRecommendId(shop.getRecommendId());
                    addOrderVo.setFeeType(shop.getFeeType());

                    if (shop.getFeeType()==1){


                        addOrderVo.setRecommendActualSum(new BigDecimal(0));
                        addOrderVo.setPlatformActualSum(new BigDecimal(0));
                        addOrderVo.setShopActualSum(new BigDecimal(0));

                      //  shopService.addWallet(shop.getShopId(),new BigDecimal(0));

                     //   shopService.addWallet( shop.getShopId(),goods.getGoodsPrice().multiply(new BigDecimal(-0.994)));

                    } else if (shop.getFeeType()==2) {


                        BigDecimal recommendActualSum = goods.getGoodsPrice().multiply(new BigDecimal(0.0057));
                        BigDecimal platformActualSum = goods.getGoodsPrice().multiply(new BigDecimal(0.0228));
                      //  BigDecimal shopActualSum = goods.getGoodsPrice().subtract(recommendActualSum).subtract(platformActualSum);

                        BigDecimal shopActualSum = goods.getGoodsPrice().multiply(new BigDecimal(-0.0285));

                        addOrderVo.setShopActualSum(shopActualSum);
                        addOrderVo.setRecommendActualSum(recommendActualSum);
                        addOrderVo.setPlatformActualSum(platformActualSum);

                        userService.addWallet(shop.getRecommendId(),recommendActualSum);



                        shopService.addWallet(shop.getShopId(),shopActualSum);



                    }

                }


                //抖音核销 1 年费 2 分成
//                if (dyGoodsVo.getFeeType()!=null){
//
//                    addOrderVo.setFeeType(dyGoodsVo.getFeeType());
//
//                    if (dyGoodsVo.getFeeType()==1){
//
//
//                        addOrderVo.setRecommendActualSum(new BigDecimal(0));
//                        addOrderVo.setPlatformActualSum(new BigDecimal(0));
//                        addOrderVo.setShopActualSum(new BigDecimal(0));
//
//
//                    } else if (dyGoodsVo.getFeeType()==2) {
//
//
//                        BigDecimal platformActualSum = goods.getGoodsPrice().multiply(new BigDecimal(0.0228));
//                        BigDecimal recommendActualSum = goods.getGoodsPrice().multiply(new BigDecimal(0.00057));
//                        BigDecimal shopActualSum = goods.getGoodsPrice().subtract(recommendActualSum).subtract(platformActualSum);
//
//
//                        addOrderVo.setShopActualSum(shopActualSum);
//                        addOrderVo.setRecommendActualSum(recommendActualSum);
//                        addOrderVo.setPlatformActualSum(platformActualSum);
//
//
//
//                    }
//                }

                billiardsService.openBilliards(openBilliardsVo);

            }



           return orderService.addOrder(addOrderVo);


        }else {

            System.out.println("失败");

            return false;
        }


    }

    @Override
    public Boolean mtValid(DyGoodsVo dyGoodsVo) {



        if (billiardsService.getBilliardsOpenStatus(dyGoodsVo.getBilliardsId())){
            throw new BaseException(BaseErrorEnum.OPEN_ERROR);
        }

        dyGoodsVo.setCode(dyGoodsVo.getCode().replaceAll(" ", ""));

        System.out.println("dyGoodsVoasdasdasdasdasd");
        System.out.println(dyGoodsVo);


       Mt mt = mtService.selectShopIdMt(dyGoodsVo.getShopId());

       if (mt==null){
           return false;
       }

        System.out.println("dsadasfasfasfazzz");
        System.out.println(mt);
       //   selectShopIdMt

       OpenBilliardsVo openBilliardsVo =  MtUtils.valid(dyGoodsVo,mt);



        if (openBilliardsVo!=null){



            System.out.println("验证了");

            AddOrderAdminVo addOrderVo = new AddOrderAdminVo();

            addOrderVo.setPhone(dyGoodsVo.getPhone());
            addOrderVo.setOrderId(Tool.getOrderNum());
            addOrderVo.setPlayWay("美团核销");
            addOrderVo.setCreateTime(LocalDateTime.now());
            addOrderVo.setGoodsId(openBilliardsVo.getGoodsId());
            addOrderVo.setOrderStatus(1);


            addOrderVo.setBilliardsNumber(openBilliardsVo.getBilliardsNumber());
            addOrderVo.setDeviceNumber(openBilliardsVo.getDeviceNumber());
            addOrderVo.setBilliardsId(openBilliardsVo.getBilliardsId());

            addOrderVo.setShopId(dyGoodsVo.getShopId());
            addOrderVo.setUserId(dyGoodsVo.getUserId());
//            addOrderVo.setFeeType(dyGoodsVo.getFeeType());
//
//
//
//            //美团核销
//            addOrderVo.setShopActualSum(new BigDecimal(1));
//            addOrderVo.setRecommendActualSum(new BigDecimal(1));
//            addOrderVo.setPlatformActualSum(new BigDecimal(1));




            Goods goods =  goodsService.selectGoods(openBilliardsVo.getGoodsId());



            if (goods!=null){

             //   Hardware hardware = hardwareService.getSubHardware(dyGoodsVo.getShopId());

                addOrderVo.setGoodsName(goods.getGoodsName());
                addOrderVo.setActualPrice( goods.getGoodsPrice());
                addOrderVo.setBilliardsHour(goods.getPlayHour());

                addOrderVo.setStartTime(LocalDateTime.now());
                addOrderVo.setEndTime(LocalDateTime.now().plusHours(goods.getPlayHour()));
                openBilliardsVo.setHour(goods.getPlayHour());

                openBilliardsVo.setHardwareId(dyGoodsVo.getHardwareId());



                Shop shop = shopService.getShop(dyGoodsVo.getShopId());

                //美团核销  1 年费 2 分成
                if (shop!=null){
                    addOrderVo.setRecommendId(shop.getRecommendId());
                    addOrderVo.setFeeType(shop.getFeeType());

                    if (shop.getFeeType()==1){


                        addOrderVo.setRecommendActualSum(new BigDecimal(0));
                        addOrderVo.setPlatformActualSum(new BigDecimal(0));
                        addOrderVo.setShopActualSum(new BigDecimal(0));

                      //  shopService.addWallet(shop.getShopId(),goods.getGoodsPrice().multiply(new BigDecimal(-0.994)));

                      //  shopService.addWallet(shop.getShopId(),new BigDecimal(0));


                    } else if (shop.getFeeType()==2) {


                        BigDecimal recommendActualSum = goods.getGoodsPrice().multiply(new BigDecimal(0.00558));

                        BigDecimal platformActualSum = goods.getGoodsPrice().multiply(new BigDecimal(0.02232));


                   //     BigDecimal shopActualSum = goods.getGoodsPrice().subtract(recommendActualSum).subtract(platformActualSum);

                        BigDecimal shopActualSum = goods.getGoodsPrice().multiply(new BigDecimal(-0.0279));


                        addOrderVo.setShopActualSum(shopActualSum);
                        addOrderVo.setRecommendActualSum(recommendActualSum);
                        addOrderVo.setPlatformActualSum(platformActualSum);



                        userService.addWallet(shop.getRecommendId(),recommendActualSum);

                 //       shopService.addWallet(shop.getShopId(),shopActualSum.multiply(new BigDecimal(-1)));

                        shopService.addWallet(shop.getShopId(),shopActualSum);

                    }

                }




                billiardsService.openBilliards(openBilliardsVo);

            }

            return orderService.addOrder(addOrderVo);


        }else {

            System.out.println("失败");

            return false;
        }
    }

    @Override
    public Dy selectDy(Integer shopId) {
        QueryWrapper<Dy> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("shop_id",shopId);
        return dyMapper.selectOne(queryWrapper);

    }



    @Override
    public IPage<DyBO> getDyPage(GetDyVo getDyVo) {
        System.out.println("getDyVo");
        System.out.println(getDyVo);
        QueryWrapper<Dy> query = new QueryWrapper<>();

        //新建分页条件
        Page<Dy> p = new Page<>(getDyVo.getCurrentPage(),getDyVo.getPageSize());
        IPage<Dy> userIPage = dyMapper.selectPage(p,query);
        IPage<DyBO> page = new Page<>();
        List<DyBO> adminDetailBOList = new ArrayList<>();
        for (Dy admin: userIPage.getRecords()){
            DyBO adminDetailBO = new DyBO();
            BeanUtils.copyProperties(admin,adminDetailBO);
            adminDetailBOList.add(adminDetailBO);
        }
        /* 将转化的BO放到的Records里面  */
        page.setRecords(adminDetailBOList);
        page.setCurrent(userIPage.getCurrent());
        page.setPages(userIPage.getPages());
        page.setPages(userIPage.getSize());
        page.setTotal(userIPage.getTotal());

        return page;
    }

    @Override
    public Boolean updateDy(UpdateDyVO updateDyVO) {


        QueryWrapper<Dy> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("dy_id",updateDyVO.getDyId());
        Dy dy = dyMapper.selectOne(queryWrapper);

        BeanUtils.copyProperties(updateDyVO,dy);
        return dyMapper.update(dy, queryWrapper) == 1;
    }

    @Override
    public Boolean addDy(AddDyVo addDyVo) {
        System.out.println("addDyVo");
        System.out.println(addDyVo);
        Dy dy = new Dy();
        BeanUtils.copyProperties(addDyVo,dy);

        System.out.println("dasasfasf");
        System.out.println(dy);

        return dyMapper.insert(dy) == 1;
    }

    @Override
    public Boolean deleteDy(Integer dyId) {
        QueryWrapper<Dy> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("dy_id",dyId);
        return dyMapper.delete(queryWrapper) == 1;
    }


}
